using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Ccr.Core;
using Microsoft.Dss.Core.Attributes;
using Microsoft.Dss.ServiceModel.Dssp;
using Microsoft.Dss.ServiceModel.DsspServiceBase;
using W3C.Soap;
using Counter = Bettzueche.Robotics.CommPerformanceCounter.Proxy;
using System.Threading;

namespace Bettzueche.Robotics.CommPerformanceDriver {
    [Contract(Contract.Identifier)]
    [DisplayName("CommPerformanceDriver")]
    [Description("Sends as fast as possible messages to the CommPerformanceCounter")]
    class CommPerformanceDriverService : DsspServiceBase {

        ///// <summary>endless Thread, sending IncrementCount requests to CounterService</summary>
        //Thread _senderT;

        /// <summary>
        /// Service state
        /// </summary>
        [ServiceState]
        CommPerformanceDriverState _state = new CommPerformanceDriverState();

        /// <summary>
        /// Main service port
        /// </summary>
        [ServicePort("/CommPerformanceDriver", AllowMultipleInstances = false)]
        CommPerformanceDriverOperations _mainPort = new CommPerformanceDriverOperations();

        /// <summary>
        /// CommPerformanceCounterService partner
        /// </summary>
        [Partner("CommPerformanceCounterService", Contract = Counter.Contract.Identifier, CreationPolicy = PartnerCreationPolicy.UseExistingOrCreate)]
        Counter.CommPerformanceCounterOperations _commPerformanceCounterServicePort = new Counter.CommPerformanceCounterOperations();

        /// <summary>
        /// Service constructor
        /// </summary>
        public CommPerformanceDriverService(DsspServiceCreationPort creationPort)
            : base(creationPort) {
        }

        protected override void Start() {
            //this.initMsgSender();
            base.Start();
            base.Activate(new IterativeTask(sendIterator));

            //_senderT.Start(); //start torturing CounterService
        }

        /// <summary>
        /// Service Handler.
        /// </summary>
        /// <param name="request">Request Instance</param>
        IEnumerator<ITask> sendIterator() {
            while (true) {
                Counter.IncrementCount countReq = new Counter.IncrementCount();
                _commPerformanceCounterServicePort.Post(countReq);
                yield return countReq.ResponsePort.Choice();
                Counter.Success success = (Counter.Success)countReq.ResponsePort;
                if (success == null) {
                    Fault flt = (Fault)countReq.ResponsePort;
                    LogError(flt);
                    yield break;
                }
            }
        }

        //protected override void Stop() {
        //    _senderT.Abort();
        //    base.Stop();
        //}

        //void initMsgSender() {
        //    _senderT = new Thread(new ThreadStart(
        //        () =>
        //        {
        //            var countReq = new Counter.IncrementCount();
        //            //while (Thread.CurrentThread.ThreadState != ThreadState.Aborted)
        //            while (true) {
        //                this._commPerformanceCounterServicePort.Post(countReq);
        //            }
        //        }
        //    ));
        //}
    }
}
